pkgs <- c("tidyverse", "gapminder")
install.packages(pkgs)library(tidyverse)
library(gapminder)| 函數 | 用途 |
|---|---|
filter() |
觀測值選擇 |
select() |
變數選擇 |
mutate() |
新增變數 |
arrange() |
排序 |
summarise() |
聚合 |
group_by() |
分組 |
load(url("https://storage.googleapis.com/r_rookies/straw_hat_df.RData"))straw_hat_df %>%
dim()
straw_hat_df %>%
View()
straw_hat_df %>%
summary()
straw_hat_df %>%
str()filter()straw_hat_df %>%
filter(gender == "Female")## name gender occupation bounty age birthday height
## 1 Nami Female Navigator 6.6e+07 20 07-03 170
## 2 Nico Robin Female Archaeologist 1.3e+08 30 02-06 188
straw_hat_df %>%
filter(gender == "Female" & age >= 30)## name gender occupation bounty age birthday height
## 1 Nico Robin Female Archaeologist 1.3e+08 30 02-06 188
select()straw_hat_df %>%
select(name, gender)## name gender
## 1 Monkey D. Luffy Male
## 2 Roronoa Zoro Male
## 3 Nami Female
## 4 Usopp Male
## 5 Vinsmoke Sanji Male
## 6 Tony Tony Chopper Male
## 7 Nico Robin Female
## 8 Franky Male
## 9 Brook Male
# 選出女性船員,但只回傳姓名就好
straw_hat_df %>%
filter(gender == "Female") %>%
select(name)## name
## 1 Nami
## 2 Nico Robin
mutate()age 減去 2straw_hat_df %>%
mutate(age_2yr_ago = age - 2)## name gender occupation bounty age birthday height
## 1 Monkey D. Luffy Male Captain 5.00e+08 19 05-05 174
## 2 Roronoa Zoro Male Swordsman 3.20e+08 21 11-11 181
## 3 Nami Female Navigator 6.60e+07 20 07-03 170
## 4 Usopp Male Sniper 2.00e+08 19 04-01 176
## 5 Vinsmoke Sanji Male Cook 1.77e+08 21 03-02 180
## 6 Tony Tony Chopper Male Doctor 1.00e+02 17 12-24 90
## 7 Nico Robin Female Archaeologist 1.30e+08 30 02-06 188
## 8 Franky Male Shipwright 9.40e+07 36 03-09 240
## 9 Brook Male Musician 8.30e+07 90 04-03 277
## age_2yr_ago
## 1 17
## 2 19
## 3 18
## 4 17
## 5 19
## 6 15
## 7 28
## 8 34
## 9 88
arrange()age 排序straw_hat_df %>%
arrange(age)## name gender occupation bounty age birthday height
## 1 Tony Tony Chopper Male Doctor 1.00e+02 17 12-24 90
## 2 Monkey D. Luffy Male Captain 5.00e+08 19 05-05 174
## 3 Usopp Male Sniper 2.00e+08 19 04-01 176
## 4 Nami Female Navigator 6.60e+07 20 07-03 170
## 5 Roronoa Zoro Male Swordsman 3.20e+08 21 11-11 181
## 6 Vinsmoke Sanji Male Cook 1.77e+08 21 03-02 180
## 7 Nico Robin Female Archaeologist 1.30e+08 30 02-06 188
## 8 Franky Male Shipwright 9.40e+07 36 03-09 240
## 9 Brook Male Musician 8.30e+07 90 04-03 277
summarise()mean_agestraw_hat_df %>%
summarise(mean_age = mean(age))## mean_age
## 1 30.33333
group_by()mean_agestraw_hat_df %>%
group_by(gender) %>%
summarise(mean_age = mean(age))## # A tibble: 2 x 2
## gender mean_age
## <fctr> <dbl>
## 1 Female 25.00000
## 2 Male 31.85714
gapminder %>%
dim()
gapminder %>%
View()
gapminder %>%
summary()
gapminder %>%
str()gapminder %>%
filter(___ == "___")gapminder %>%
filter(___ == ___) %>%
summarise(ttl_pop = sum(___))gapminder %>%
filter(___ == ___) %>%
group_by(___) %>%
summarise(ttl_pop = sum(___)) %>%
arrange(___)gapminder_2007 <- gapminder %>%
filter(year == 2007)
scatter <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, colour = continent)) +
geom_point() +
theme_minimal()scatternorth_asia <- gapminder %>%
filter(country %in% c("China", "Japan", "Taiwan", "Korea, Rep."))
line_plot <- ggplot(north_asia, aes(x = year, y = gdpPercap, colour = country)) +
geom_line() +
theme_minimal()line_plothistogram <- ggplot(gapminder_2007, aes(x = gdpPercap)) +
geom_histogram(bins = 20, fill = rgb(1, 0, 0, 0.5)) +
theme_minimal()histogrambox_plot <- ggplot(gapminder_2007, aes(x = continent, y = gdpPercap, colour = continent)) +
geom_boxplot() +
theme_minimal() +
theme(legend.position = "none")box_plotgdpPercap_2007_na <- gapminder %>%
filter(year == 2007 & country %in% c("China", "Japan", "Taiwan", "Korea, Rep.")) %>%
arrange(gdpPercap)
gdpPercap_2007_na$country <- factor(gdpPercap_2007_na$country, levels = gdpPercap_2007_na$country)
bar_plot <- ggplot(gdpPercap_2007_na, aes(x = country, y = gdpPercap, fill = country, alpha = 0.5)) +
geom_bar(stat = "identity") +
coord_flip() +
theme_minimal() +
theme(legend.position = "none")bar_plot